package org.example.review.controller;

import com.common.pojo.entity.OperationLog;
import com.common.result.Result;
import org.example.review.dto.WorkDTO;
import org.example.review.mapper.OperationLogMapper;
import org.example.review.service.OperaService;
import org.example.review.service.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.logging.Logger;

@RestController
@RequestMapping("/work")
public class WorkController {

    private static final Logger logger = Logger.getLogger(WorkController.class.getName());

    @Autowired
    private WorkService workService;

    @Autowired
    private OperationLogMapper operationLogMapper;

    /**
     * 获取工作列表
     * @return 工作列表
     */
    @GetMapping("/list")
    public Result<List<WorkDTO>> getWorkList() {
        logger.info("接收到获取工作列表请求");
        try {
            List<WorkDTO> workList = workService.getWorkList();
            return Result.success(workList);
        } catch (Exception e) {
            logger.severe("获取工作列表失败: " + e.getMessage());
            return Result.error("获取工作列表失败: " + e.getMessage());
        }
    }

    /**
     * 审核通过工作项
     * @param id 工作项ID
     * @param remark 备注信息（可选）
     * @return 操作结果
     */
    @PostMapping("/approve")
    public Result<String> approveWork(@RequestParam Integer id,
                                     @RequestParam(required = false) String remark) {
        logger.info("接收到审核通过请求，ID: " + id + ", 备注: " + remark);
        try {
            boolean success = workService.approveWork(id);
            if (success) {
                Map<String, Object> result = new HashMap<>();
                result.put("id", id);
                result.put("action", "approve");
                result.put("success", true);
                result.put("remark", remark);
                result.put("message", "审核通过成功");
                return Result.success("审核通过成功");
            } else {
                return Result.error("审核通过失败，可能状态不允许或ID不存在");
            }
        } catch (Exception e) {
            logger.severe("审核通过失败: " + e.getMessage());
            return Result.error("审核通过失败: " + e.getMessage());
        }
    }

    /**
     * 审核驳回工作项
     * @param id 工作项ID
     * @param reason 驳回原因（可选）
     * @return 操作结果
     */
    @PostMapping("/reject")
    public Result<String> rejectWork(@RequestParam Integer id,
                                   @RequestParam(required = false) String reason) {
        logger.info("接收到审核驳回请求，ID: " + id + ", 原因: " + reason);
        try {
            boolean success = workService.rejectWork(id, reason);
            if (success) {
                Map<String, Object> result = new HashMap<>();
                result.put("id", id);
                result.put("action", "reject");
                result.put("success", true);
                result.put("reason", reason);
                result.put("message", "审核驳回成功");
                return Result.success("审核驳回成功");
            } else {
                return Result.error("审核驳回失败，可能状态不允许或ID不存在");
            }
        } catch (Exception e) {
            logger.severe("审核驳回失败: " + e.getMessage());
            return Result.error("审核驳回失败: " + e.getMessage());
        }
    }

    /**
     * 更新工作项状态（通用接口）
     * @param id 工作项ID
     * @param status 新状态 (1=rejected, 2=pending, 3=approved)
     * @return 操作结果
     */
    @PutMapping("/update")
    public Result<Map<String, Object>> handleWorkItem(@RequestParam Integer id,
                                                      @RequestParam Integer status) {
        logger.info("接收到更新工作项状态请求，ID: " + id + ", 状态: " + status);

        try {
            boolean success;
            String action;

            if (status == 3) {
                // 审核通过
                success = workService.approveWork(id);
                action = "approve";
            } else if (status == 1) {
                // 审核驳回
                success = workService.rejectWork(id, "状态更新为驳回");
                action = "reject";
            } else {
                return Result.error("不支持的状态值: " + status + "。支持的值：1(驳回), 3(通过)");
            }

            Map<String, Object> result = new HashMap<>();
            result.put("id", id);
            result.put("action", action);
            result.put("status", status);
            result.put("success", success);
            result.put("timestamp", System.currentTimeMillis());

            if (success) {
                result.put("message", "状态更新成功");
                return Result.success(result);
            } else {
                return Result.error("状态更新失败，可能状态不允许或ID不存在");
            }

        } catch (Exception e) {
            logger.severe("更新工作项状态失败: " + e.getMessage());
            return Result.error("更新工作项状态失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作项的操作日志
     * @param applyId 申请ID
     * @return 操作日志列表
     */

}